package cn.smbms.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.smbms.biz.ProviderBiz;
import cn.smbms.entity.Provider;
import cn.smbms.entity.User;
import utils.Counts;

@Controller
@RequestMapping("/provider")
public class ProviderControll {
	
	@Autowired
	ProviderBiz providerBiz;
	
	@RequestMapping("/getProviderList")
	public String getProviderList(@RequestParam(value="queryProCode",required=false) String proCode,
			@RequestParam(value="queryProName",required=false) String proName,Model model) throws Exception {
		
		List<Provider> list = providerBiz.getProviderList(proName, proCode);
		
		model.addAttribute("providerList", list);
		model.addAttribute("queryProCode", proCode);
		model.addAttribute("queryProName", proName);
		
		return "providerlist";
	} 
	
	@RequestMapping(value="addProvider",method=RequestMethod.GET)
	public String toAddProvider() {
		return "provideradd";
	}
	
	@RequestMapping(value="addProvider",method=RequestMethod.POST)
	public String addProvider(@Valid @ModelAttribute Provider provider,BindingResult bindingResult,HttpSession session) throws Exception {
		if(bindingResult.hasErrors()) {
			return "provideradd";
		}
		User u = (User)session.getAttribute(Counts.USER_SESSION);
		provider.setCreatedBy(u.getId());
		provider.setCreationDate(new Date());
		int n = providerBiz.add(provider);
		System.out.println(n);
		if(n>0) {
			return "redirect:/provider/getProviderList";
		}else {
			return "provideradd";
		}
		
	}
	
	@RequestMapping(value="/view")
	public String view(@RequestParam("proid")Integer id,Model model) throws Exception {
		Provider provider = providerBiz.getProviderById(id);
		model.addAttribute("provider", provider);
		return "providerview";
	}
	
	@RequestMapping(value="/modify",method=RequestMethod.GET)
	public String toModify(@RequestParam("proid") Integer id,Model model) throws Exception {
		Provider provider = providerBiz.getProviderById(id);
		model.addAttribute("provider", provider);
		return "providermodify";
	}
	
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	public String doModify(Provider provider,Model model) throws Exception {
		int n = providerBiz.modify(provider);
		if(n>0) {
			return "redirect:/provider/getProviderList";
		}else {
			model.addAttribute("provider", provider);
			return "providermodify";
		}
	}
	
	@RequestMapping("/delete")
	public String delete(@RequestParam("proid") Integer id,Model model) throws Exception {
		int n = providerBiz.deleteProviderById(id);
		if(n>0) {
			model.addAttribute("msg", "删除成功");
		}else {
			model.addAttribute("msg", "删除失败");
		}
		return "redirect:/provider/getProviderList";
	}
}
